package com.onetech.adrewards.free.adapters;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fedorvlasov.lazylist.ImageLoader;
import com.onetech.adrewards.free.objects.AdDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.onetech.adrewards.free.R;


public class ListAdRewardAdapter extends BaseAdapter {
	private class AppViewHolder {
		public TextView title;
		public TextView charge_type;
		public TextView category;
		public TextView max_point;
		public TextView chapterNum;
		public ImageView cover;
	}

	private Activity activity;
	private List<AdDTO> listApps;
	private static LayoutInflater inflater = null;

	public ListAdRewardAdapter(Activity _activity) {
		activity = _activity;
		listApps = new ArrayList<AdDTO>();
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void updateAppList(List<AdDTO> _listAppDTOs) {
		listApps.clear();
		listApps.addAll(_listAppDTOs);
		notifyDataSetChanged();
	}

	public void insertAppList(List<AdDTO> _listAppDTOs) {
		listApps.addAll(_listAppDTOs);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listApps.size();
	}

	@Override
	public AdDTO getItem(int arg0) {
		if (listApps == null) {
			return null;
		}
		return listApps.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		AppViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_row_list_adreward,
					parent, false);
			viewHolder = new AppViewHolder();
			viewHolder.cover = (ImageView) convertView
					.findViewById(R.id.appCover);
			viewHolder.title = (TextView) convertView
					.findViewById(R.id.appName);	
			viewHolder.charge_type = (TextView) convertView
					.findViewById(R.id.charge_type);
			viewHolder.max_point = (TextView) convertView
					.findViewById(R.id.appPoints);
//			viewHolder.date_created = (TextView) convertView
//					.findViewById(R.id.dateCreated);	
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (AppViewHolder) convertView.getTag();
		}
		AdDTO temp = listApps.get(position);
		viewHolder.title.setText(temp.getContent_name());
		viewHolder.charge_type.setText(AdrewadsUtils.getChargeTypeText(activity,temp.getCharge_tpe()) + AdrewadsUtils.getAction_Cate_ID_Text(activity,temp.getAction_cate_id()));
		viewHolder.max_point.setText(temp.getMax_point()+" pt");
		//viewHolder.date_created.setText(temp.getDis_expired_end_date());
		ImageLoader.getInstance(activity.getApplicationContext()).DisplayImage(
				temp.getBanner_img_1(), viewHolder.cover);

		return convertView;
	}

}

